/**
* @description: 154. 寻找旋转排序数组中的最小值 II
* @author hewei
* @date 2022/7/28 14:26
* @version 1.0
*/

public class FindMin2 {

    public int findMin(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        int ans = Integer.MAX_VALUE;
        while (l <= r) {
            int mid = (r - l) / 2 + l;
            if (nums[l] == nums[r]) {
                ans = Math.min(ans, nums[l]);
                ++l;
                --r;
            } else if (nums[mid] >= nums[l]) {
                ans = Math.min(nums[l], ans);
                l = mid + 1;
            } else {
                ans = Math.min(nums[mid], ans);
                r = mid - 1;
            }
        }
        return ans;
    }
}
